﻿package 
{
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	
	public class Main3 extends Sprite {
		private var rootMc	:MovieClip;
		
		public function Main3():void {
			rootMc = addChild(new MovieClip) as MovieClip;
			rootMc.addChild(new Shape()).parent.transform.matrix = new Matrix(-1, 0, 0, -1, Math.round(stage.stageWidth / 2), stage.stageHeight - 20);
		
			var o:Object = {lengthRandom:0.2, level:1, thickness:1 };
			for (var name:String in o) { rootMc[name] = o[name] };
			
			stage.addEventListener(Event.ENTER_FRAME, redraw);
		}
		
		private function redraw(e:Event = null):void  { if (rootMc.thickness++ < 120) updateLine(rootMc, rootMc.thickness); }
		
		public function updateLine(thisMc:MovieClip, thickness:int):MovieClip {
			thisMc.thickness = thickness;
			thisMc.lineHeight = Math.round( thisMc.lengthRandom * 15 * (thisMc.thickness - 3));
			
			(thisMc.getChildAt(0) as Shape).graphics.clear();
			(thisMc.getChildAt(0) as Shape).graphics.lineStyle(thisMc.thickness/10, 0x1C6425);
			(thisMc.getChildAt(0) as Shape).graphics.lineTo( 0, thisMc.lineHeight / 10);
		//	trace(thisMc.requiredLength, thisMc.length);
			if (thisMc.thickness > 6 && thisMc.level < 8) {
				var i:int;
				var lineMc:MovieClip;
				if (thisMc.numChildren <= 1) for (i = (thisMc.level <= 2)? thisMc.level + 1 : 3; i >= 0; i--) {
					var shape:Shape = (thisMc.addChild(new MovieClip()) as MovieClip).addChild(new Shape()) as Shape;
				//	var o:Object = {lengthRandom: Math.round(Math.random() * 2 + ((thisMc.level < 5)?thisMc.level:5) )/10, thicknessMultiplication: Math.round(5 + Math.random() * ((thisMc.level < 5)?thisMc.level:5))/10, posRandom: (i <= 1) ? 1 - Math.random() * 0.3*i : Math.round(4 + Math.random() * 6) / 10, level: thisMc.level+1, rotation: Math.round( (thisMc.level - 1) * -10 - 20  + Math.random() * (((thisMc.level - 1) * 20) + 40)) };
				//	var o:Object = {lengthRandom: Math.round(4 + Math.random() * ((thisMc.level < 5)?thisMc.level/2:3) )/10, thicknessMultiplication: Math.round(5 + Math.random() * ((thisMc.level < 5)?thisMc.level:5))/10, posRandom: (i <= 1) ? 1 - Math.random() * 0.3*i : Math.round(4 + Math.random() * 6) / 10, level: thisMc.level+1, rotation: Math.round( (thisMc.level - 1) * -10 - 20  + Math.random() * (((thisMc.level - 1) * 20) + 40)) };
				//	var o:Object = {lengthRandom: Math.round(4 + Math.random() * 4)/10, thicknessMultiplication: Math.round(4 + Math.random() * 4)/10, posRandom: (i <= 1) ? 1 - Math.random() * 0.3*i : Math.round(4 + Math.random() * 6) / 10, level: thisMc.level+1, rotation: Math.round( (thisMc.level - 1) * -20 - 30  + Math.random() * (((thisMc.level - 1) * 40) + 60)) };
					var o:Object = {requiredLength:(8 - thisMc.level) *5, lengthRandom: Math.round(4 + Math.random() * 4)/10, thicknessMultiplication: Math.round(4 + Math.random() * 4)/10, posRandom: (i <= 1) ? 1 - Math.random() * 0.3*i : Math.round(4 + Math.random() * 6) / 10, level: thisMc.level+1, rotation: Math.round( (thisMc.level - 1) * 10  + Math.random() * ((thisMc.level - 1) * 20))*((Math.random() < 0.5)?-1 : 1) };
					for (var name:String in o) { shape.parent[name] = o[name] };
				}
				for (i = thisMc.numChildren - 1; i >= 1; i--) {
					lineMc = thisMc.getChildAt(i) as MovieClip;
					updateLine(lineMc, (((thisMc.thickness - 6) * lineMc.thicknessMultiplication) > 0.1)? ((thisMc.thickness - 6) * lineMc.thicknessMultiplication) : 0.1).y = thisMc.lineHeight/10 * lineMc.posRandom;
				}
			}
			return thisMc;
		}
	}
}